package ru.tulupov.guessgame;

import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Parser {
	private Scanner in;

	/**
	 * Constructs parser using input stream.
	 * 
	 * @param is
	 *            the input stream.
	 */
	public Parser(InputStream is) {
		this.in = new Scanner(is);
	}

	/**
	 * Parse integer value from stream.
	 * 
	 * @return the integer value.
	 * @throws ParseException
	 * @throws EOFParseException
	 */
	public int parse() {
		int value = 0;

		try {
			value = in.nextInt();
		} catch (InputMismatchException e) {
			// skip invalid token
			in.next();
			throw new ParseException();
		} catch (NoSuchElementException e) {
			throw new EOFParseException();
		}

		return value;
	}
}
